package com.lesso.lll.sunflower.employee.service.imp;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lesso.lll.sunflower.employee.entity.Employee;
import com.lesso.lll.sunflower.employee.repository.EmployeeRepository;
import com.lesso.lll.sunflower.employee.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 员工服务实现类
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeRepository, Employee> implements EmployeeService {

    @Autowired
    private EmployeeRepository employeeRepository;

    @Override
    @Transactional
    public Long addEmployee(Employee employee) {
        // 检查身份证是否已存在
        Employee existEmployee = employeeRepository.selectByIdCard(employee.getIdCard());
        if (existEmployee != null) {
            throw new BusinessException("身份证号已存在");
        }

        // 生成唯一工号
        String employeeId = generateEmployeeId();
        employee.setEmployeeId(employeeId);

        // 设置默认状态为在职
        employee.setStatus(1);
        employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());

        // 保存员工信息
        int result = employeeRepository.insert(employee);
        if (result > 0) {
            return employee.getId();
        }
        return null;
    }

    @Override
    @Transactional
    public boolean updateEmployee(Employee employee) {
        if (employee.getId() == null) {
            throw new BusinessException("员工ID不能为空");
        }

        // 只允许修改手机号和邮箱
        Employee updateEmployee = new Employee();
        updateEmployee.setId(employee.getId());
        updateEmployee.setPhone(employee.getPhone());
        updateEmployee.setEmail(employee.getEmail());
        updateEmployee.setUpdateTime(new Date());

        int result = employeeRepository.updateById(updateEmployee);
        return result > 0;
    }

    @Override
    @Transactional
    public boolean resignEmployee(Long employeeId) {
        Employee employee = employeeRepository.selectById(employeeId);
        if (employee == null) {
            throw new BusinessException("员工不存在");
        }

        // 检查员工状态
        if (employee.getStatus() == 2) {
            throw new BusinessException("员工已离职");
        }

        // 修改员工状态为离职
        Employee updateEmployee = new Employee();
        updateEmployee.setId(employeeId);
        updateEmployee.setStatus(2);
        updateEmployee.setUpdateTime(new Date());

        int result = employeeRepository.updateById(updateEmployee);
        return result > 0;
    }

    @Override
    @Transactional
    public boolean bindAttendanceDevices(Long employeeId, List<Long> deviceIds) {
        // 实现员工与考勤设备的绑定逻辑
        // 此处省略具体实现，需要操作EmployeeDevice关联表
        return true;
    }

    @Override
    @Transactional
    public boolean bindAttendanceRule(Long employeeId, Long ruleId, String effectiveDate) {
        // 实现员工与考勤规则的绑定逻辑
        // 此处省略具体实现，需要操作EmployeeRule关联表
        return true;
    }

    @Override
    public boolean isDeviceAllowed(Long employeeId, Long deviceId) {
        // 验证设备是否允许该员工打卡
        // 此处省略具体实现，需要查询EmployeeDevice关联表
        return true;
    }

    /**
     * 生成唯一工号
     * @return 工号
     */
    private String generateEmployeeId() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String datePart = sdf.format(new Date());
        String randomPart = UUID.randomUUID().toString().substring(0, 4).toUpperCase();
        return "LES" + datePart + randomPart;
    }
}    